export interface ConditionItemType {
  key: string;
  type: string;
  label: string;
  controlType: string;
  controlId: string;
  options: { text: string; value: string | number }[];
  mode: 'singleSelectbox' | 'multipleSelectbox' | 'single' | 'multiple' | '';
  value: any;
}

// 文件基本schema
export const fileschemaOptions: ConditionItemType[] = [
  {
    // 内容
    key: 'filecontent',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.content',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox0',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 所在文件夹
    key: 'filepath',
    type: 'EDoc2.Meta.Folder',
    label: 'SearchEngine.folderIn',
    controlType: 'edoc2SelectFolder',
    controlId: 'edoc2SelectFolder1',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 后缀名
    key: 'fileextname',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.fileType',
    controlId: 'edoc2Textbox2',
    controlType: 'edoc2Selectbox',
    options: [],
    mode: 'singleSelectbox',
    value: '',
  },
  {
    // 创建人
    key: 'filecreateoperator',
    type: 'EDoc2.Meta.Member',
    label: 'SearchEngine.creator',
    controlType: 'edoc2SelectMember',
    controlId: 'edoc2SelectMember3',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 创建时间
    key: 'filecreatetime',
    type: 'EDoc2.Meta.DateTime',
    label: 'SearchEngine.creationTime',
    controlType: 'edoc2Date',
    controlId: 'edoc2Date4',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 文件名
    key: 'filename',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.fileName',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox5',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 编号
    key: 'filecode',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.number',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox6',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 修改人
    key: 'filemodifyoperator',
    type: 'EDoc2.Meta.Member',
    label: 'SearchEngine.modifiedBy',
    controlType: 'edoc2SelectMember',
    controlId: 'edoc2SelectMember7',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 修改时间
    key: 'filemodifytime',
    type: 'EDoc2.Meta.DateTime',
    label: 'SearchEngine.modifiedOn',
    controlType: 'edoc2Date',
    controlId: 'edoc2Date8',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 密级
    key: 'securitylevelid',
    type: 'EDoc2.Meta.Dropdown',
    label: 'SearchEngine.classification',
    controlType: 'edoc2Selectbox',
    controlId: 'edoc2Selectbox9',
    options: [
      { text: 'SearchEngine.unclassified', value: 1 },
      { text: 'SearchEngine.inside', value: 2 },
      { text: 'SearchEngine.secret', value: 3 },
      { text: 'SearchEngine.confidential', value: 4 },
    ],
    mode: 'singleSelectbox',
    value: '',
  },
  {
    // 版本
    key: 'filenumstr',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.edition',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox10',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 备注
    key: 'fileremark',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.remarks',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox11',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 标签
    key: 'filetag',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.label',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox12',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 文件大小
    key: 'filesize',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.fileSize',
    controlType: 'edoc2FileSize',
    controlId: 'edoc2FileSize01',
    options: [],
    mode: '',
    value: [],
  },
];

// 文件夹基本schema
export const folderSchemaOptions: ConditionItemType[] = [
  {
    // 所在文件夹
    key: 'folderpath',
    type: 'EDoc2.Meta.Folder',
    label: 'SearchEngine.folderIn',
    controlType: 'edoc2SelectFolder',
    controlId: 'edoc2SelectFolder13',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 文件夹名
    key: 'foldername',
    type: 'EDoc2.Meta.String',
    label: 'SearchEngine.folderName',
    controlType: 'edoc2Textbox',
    controlId: 'edoc2Textbox14',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 创建人
    key: 'foldercreateoperator',
    type: 'EDoc2.Meta.Member',
    label: 'SearchEngine.creator',
    controlType: 'edoc2SelectMember',
    controlId: 'edoc2SelectMember15',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 修改人
    key: 'foldermodifyoperator',
    type: 'EDoc2.Meta.Member',
    label: 'SearchEngine.modifiedBy',
    controlType: 'edoc2SelectMember',
    controlId: 'edoc2SelectMember16',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 创建时间
    key: 'foldercreatetime',
    type: 'EDoc2.Meta.DateTime',
    label: 'SearchEngine.creationTime',
    controlType: 'edoc2Date',
    controlId: 'edoc2Date17',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 修改时间
    key: 'foldermodifytime',
    type: 'EDoc2.Meta.DateTime',
    label: 'SearchEngine.modifiedOn',
    controlType: 'edoc2Date',
    controlId: 'edoc2Date18',
    options: [],
    mode: '',
    value: '',
  },
];

// 元数据基本schema
export const metaSchema: ConditionItemType[] = [
  {
    // 数字框
    key: '',
    type: 'edoc2Number',
    controlType: 'edoc2Number',
    controlId: '',
    label: '',
    options: [],
    mode: '',
    value: '',
  },
  {
    // textarea
    key: '',
    type: 'edoc2TextArea',
    controlId: '',
    controlType: 'edoc2TextArea',
    label: '',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 富文本
    key: '',
    type: 'edoc2RichText',
    controlId: '',
    controlType: 'edoc2RichText',
    label: '',
    options: [],
    mode: '',
    value: '',
  },
  {
    // 开关
    key: '',
    type: 'edoc2Switch',
    controlId: '',
    controlType: 'edoc2Switch',
    label: '',
    options: [],
    mode: '',
    value: '',
  },
];

interface MetaType {
  AttrId: string;
  AttrName: string;
  ControlModel: {
    ControlId: string;
    ControlType: string;
    Setting: Record<string, string>[];
  };
}

export const allSchemaOptions = [...metaSchema, ...fileschemaOptions];

export type SchemaOption = typeof metaSchema[0];

const fileDefaultSchemaKey = [
  'filecreateoperator',
  'filecreatetime',
  'foldermodifyoperator',
  'filemodifytime',
  'fileextname',
  'filesize',
]; //'fileContent', 'filePath'
export const defaultFileShowSchemaOptions = fileschemaOptions
  .filter((item) => fileDefaultSchemaKey.includes(item.key))
  .map((item) => ({
    ...item,
  }));

const folderDefaultSchemaKey = [
  'foldername',
  'folderpath',
  'foldercreateoperator',
  'foldermodifyoperator',
  'foldercreatetime',
  'foldermodifytime',
];
export const defaultFolderShowSchemaOptions = folderSchemaOptions
  .filter((item) => folderDefaultSchemaKey.includes(item.key))
  .map((item) => ({
    ...item,
  }));

export const metaTransformToSchema = (meta: MetaType, typeID: string) => {
  const field = allSchemaOptions.find((item) => item.controlType === meta.ControlModel.ControlType);
  if (!field) {
    return;
  }

  const newField = { ...field, options: [...field.options] };
  const metaID = `${typeID}@${meta.AttrId}`;
  newField.key = metaID;
  newField.label = meta.AttrName;

  if (meta.ControlModel.ControlType === 'edoc2Selectbox') {
    // 含有静态元数据的字段
    const dataSource = JSON.parse(
      meta.ControlModel.Setting.find((item) => item.id === 'datasource')?.value || '[]',
    );
    const mode = meta.ControlModel.Setting.find((item) => item.id === 'mode')?.value || '';
    newField.options = dataSource;
    // @ts-ignore
    newField.mode = mode;
  }
  return newField;
};
